package com.example.zzj.ziwuyundemo.activity;


import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzj.ziwuyundemo.R;
import com.example.zzj.ziwuyundemo.base.BaseActivity;
import com.example.zzj.ziwuyundemo.entity.PcIdEntity;
import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2017/1/3 0003.
 */

public class StateQueryActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.site_back)
    ImageView siteBack;
    @InjectView(R.id.runnumber)
    TextView runnumber;
    @InjectView(R.id.carnumber)
    TextView carnumber;
    @InjectView(R.id.shougoods)
    TextView shougoods;
    @InjectView(R.id.runaway)
    TextView runaway;
    @InjectView(R.id.runpeople)
    TextView runpeople;
    @InjectView(R.id.yapeople)
    TextView yapeople;
    @InjectView(R.id.runstatus)
    TextView runstatus;


    private String userId;
    private PcIdEntity pcidEntity;
    private String pcid;
    private static final String TAG = "StateQueryActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statequery);
        ButterKnife.inject(this);
        initView();
        initData();


    }

    private void initView() {
        {
            Intent intent = getIntent();
            userId = intent.getStringExtra("userId");
            OkHttpClient okHttpClient = new OkHttpClient();
            FormEncodingBuilder builder = new FormEncodingBuilder();
            builder.add("userId", userId);
            final Request request = new Request.Builder().url("http://192.168.1.75:8080/mclcm/a/mclcm/test/getpcd").post(builder.build()).build();
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {

                }

                @Override
                public void onResponse(Response response) throws IOException {
                    String body = response.body().string();
                    Gson gson = new Gson();
                    pcidEntity = new PcIdEntity();
                    pcidEntity = gson.fromJson(body, PcIdEntity.class);
                    if (pcidEntity.isSui() == false) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(mContext, "没有数据", Toast.LENGTH_SHORT).show();
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                pcid = pcidEntity.getRecodeSendcarPlans().getId();
                                Log.i(TAG, pcid);
                                initView1();
                            }
                        });
                    }

                }
            });

        }

    }

    private void initData() {
        siteBack.setOnClickListener(this);
    }

    private void initView1() {
        Intent intent = getIntent();
        userId = intent.getStringExtra("userId");
        OkHttpClient okHttpClient = new OkHttpClient();
        FormEncodingBuilder builder = new FormEncodingBuilder();
        builder.add("userId", userId);
        builder.add("pcId", pcid);
        final Request request = new Request.Builder().url("http://192.168.1.75:8080/mclcm/a/mclcm/test/stateQuery").post(builder.build()).build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {

            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.site_back:
                finish();
                break;
        }
    }
}
